package com.kaaction.modules.crab.api;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.web.BaseController;
import com.kaaction.modules.crab.entity.Order;
import com.kaaction.modules.crab.service.CouponDetailService;
import com.kaaction.modules.crab.service.OrderService;

@Controller
@RequestMapping(value = "/api/v1/crabExchangeCouponApi")
public class CrabExchangeCouponApi extends BaseController{
	
	
	@Autowired
	CouponDetailService couponDetailService;
	
	@Autowired
	OrderService orderService;
	
	
	/**
	 * 兑换商品
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 */
	@RequestMapping(value = "/selExchangeCouponProduct")
	public void selExchangeCouponProduct(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException {
		String cardNum = request.getParameter("cardNum");
		String password = request.getParameter("password");
		super.writeAjaxResponse(response,couponDetailService.selExchangeCouponProduct(cardNum,password));
	}
	
	
	/**
	 * 订单生成
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws DocumentException
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/orderCreate")
	public void orderCreate(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, DocumentException, ParseException {
		
		String cardNum = request.getParameter("cardNum");//兑换券号
		String password = request.getParameter("password");//密码
		
		int cnt = orderService.selEqual(cardNum,password);
		
		if(cnt==0){
			Order order = new Order();
			String name = request.getParameter("name");//收货人
			String mobile = request.getParameter("mobile");//电话
			String province = request.getParameter("province");//省份
			String city = request.getParameter("city");//城市
			String county = request.getParameter("county");//区(县)
			String address = request.getParameter("address");//地址
			String hopeDate = request.getParameter("hopeDate");//期望收货日期
			String remarks = request.getParameter("remarks");//备注
			String productId = request.getParameter("productId");//兑换券号
			
			order.setName(name);
			order.setMobile(mobile);
			order.setProvince(province);
			order.setCity(city);
			order.setCounty(county);
			order.setAddress(address);
			if(StringUtils.isNotBlank(hopeDate)){
				order.setHopeDate(new SimpleDateFormat("yyyy-MM-dd").parse(hopeDate));
			}else{
				order.setHopeDate(new Date());
			}
			order.setRemarks(remarks);
			order.setCardNum(cardNum);
			order.setPassword(password);
			order.setProductId(productId);
			
			orderService.insert(order);
			super.writeAjaxResponse(response,"");
		}else{
			super.writeFailResponse(response, "", "该卡号的订单已存在");
		}
	}
}
